package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;

import prevote.PreVotePanelDescriptor;
import protocol.Info;
import publicate.PublicateResultsPanelDescriptor;
import registration.PersonalDataPanelDescriptor;
import wizard.WizardPanelDescriptor;

public class WelcomePanelDescriptor extends WizardPanelDescriptor implements ActionListener
{
    public static final String IDENTIFIER = "WELCOME_PANEL";
    private WelcomePanel wp;
    private String nextPanel = PersonalDataPanelDescriptor.IDENTIFIER;

    public WelcomePanelDescriptor()
    {
        wp = new WelcomePanel();
        wp.addRadioButtonsActionListener( this );
        setPanelDescriptorIdentifier( IDENTIFIER );
        setPanelComponent( wp );
    }

    public String getNextPanelDescriptor()
    {
        return nextPanel;
    }

    public String getBackPanelDescriptor()
    {
        return null;
    }

    public void aboutToDisplayPanel()
    {
        setNextButtonAccordingToRadioButtons();
    }

    private void setNextButtonAccordingToRadioButtons()
    {
        getWizard().setNextFinishButtonEnabled( false );
        if( wp.choice() == wp.VOTE_CHOSEN )
        {
            System.out.println( "vote" );
            nextPanel = PreVotePanelDescriptor.IDENTIFIER;
            getWizard().setNextFinishButtonEnabled( false );
            try
            {
                Socket socket = new Socket( Info.HOSTIN, Info.VOTE_PORT );
                socket.close();
                getWizard().setNextFinishButtonEnabled( true );
            }
            catch( Exception e )
            {
                System.out.println( "Vote is not aviable" );
            }
        }
        else if( wp.choice() == wp.REGISTER_CHOSEN )
        {
            System.out.println( "register" );
            nextPanel = PersonalDataPanelDescriptor.IDENTIFIER;
            getWizard().setNextFinishButtonEnabled( false );
            try
            {
                Socket socket = new Socket( Info.HOSTIN, Info.REGISTRATION_PORT );
                socket.close();
                getWizard().setNextFinishButtonEnabled( true );
            }
            catch( Exception e )
            {
                System.out.println( "Register is not aviable" );
            }
        }
        else if( wp.choice() == wp.VIEW_RESULTS_CHOSEN )
        {
            System.out.println( "view results" );
            nextPanel = PublicateResultsPanelDescriptor.IDENTIFIER;
            getWizard().setNextFinishButtonEnabled( false );
            try
            {
                Socket socket = new Socket( Info.HOSTIN, Info.PUBLISH_SERVER );
                socket.close();
                getWizard().setNextFinishButtonEnabled( true );
            }
            catch( Exception e )
            {
                System.out.println( "View result is not aviable" );
            }
        }
        else
        {
            getWizard().setNextFinishButtonEnabled( false );
        }
    }

    @Override
    public void actionPerformed( ActionEvent arg0 )
    {
        setNextButtonAccordingToRadioButtons();
    }
}
